package com.miyake.loveviewweb.client.community;

import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.client.main.UiDataHandler;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.client.service.DataServiceAsync;
import com.miyake.loveviewweb.client.viewer.AbstractListWidget;
import com.miyake.loveviewweb.client.viewer.DataViewer;
import com.miyake.loveviewweb.client.viewer.Viewer;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.GroupModel;
import com.miyake.loveviewweb.shared.model.UserModel;

public abstract class GroupPropertyPanel extends Composite {
    private DataServiceAsync service = DataService.Util.getInstance();
    abstract protected void onBack();
    
    private UiDataHandler uiHandler = new UiDataHandler() {

        public void createRootFolder() {
            // TODO Auto-generated method stub
            
        }

        public void createFolder() {
            // TODO Auto-generated method stub
            
        }

        public void createData() {
            // TODO Auto-generated method stub
            
        }

        public void createChart(DataModel dataModel) {
            // TODO Auto-generated method stub
            
        }

        public void delete() {
            // TODO Auto-generated method stub
            
        }

        public void upload() {
            // TODO Auto-generated method stub
            
        }

        public void pluginMarket() {
            // TODO Auto-generated method stub
            
        }

        public void pluginProvider() {
            // TODO Auto-generated method stub
            
        }
        
    };
    
    public GroupPropertyPanel(GroupModel groupModel){
        VerticalPanel panel = new VerticalPanel();
        initWidget(panel);
        panel.setWidth("100%");
        panel.setHeight("100%");
        Anchor anchor = new Anchor("Back to list");
        panel.add(anchor);        
        panel.add(new HTML("<h1>" + groupModel.getName() + "</h1>"));
        panel.add(new HTML(groupModel.getDescription()));

        anchor.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                onBack();
            } 
            
        });
        
        panel.add(getOwnerPanel(groupModel));
        panel.add(getUserList(groupModel));
        panel.add(getPublishedList(groupModel));

    }

    private Widget getPublishedList(GroupModel groupModel) {
        VerticalPanel panel = new VerticalPanel();
        panel.setWidth("100%");
        List<Key> list = groupModel.getPublishedData();
        
        panel.add(new HTML("<b>Published Data</b>"));
        final AbstractListWidget<DataModel> listWidget = new AbstractListWidget<DataModel>() {
            @Override
            protected Widget getWidget(DataModel dataModel) {
                VerticalPanel p = new VerticalPanel();
                final Anchor user = new Anchor();
                DataService.Util.getInstance().getUser(dataModel.getOwner(), new AsyncCallback<UserModel>() {

                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub
                        
                    }

                    public void onSuccess(UserModel result) {
                        user.setText(result.getFirstName() + "," + result.getLastName());
                    }
                    
                });
                
                Viewer viewer = new DataViewer(dataModel, uiHandler) {

                    @Override
                    protected boolean canPublish() {
                        return false;
                    }
                    
                };
                viewer.update(dataModel.getKey());
                p.add(viewer);
                
                HorizontalPanel h = new HorizontalPanel();
                h.add(new Label("Published by: "));
                h.add(user);
                p.add(h);
                
                return p;
            }
        };
        DataServiceWrapper.getInstance().getService().getDataList(list, false, new AsyncCallback<List<DataModel>>() {

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(List<DataModel> result) {
                listWidget.setElements(result);
                listWidget.update();
            }
            
        });
        listWidget.getGrid().setBorderWidth(1);
        panel.add(listWidget);
        return panel;
        
/*        DataServiceWrapper.getInstance().getService().getDataList(list, false, new AsyncCallback<List<DataModel>>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(List<DataModel> result) {
                int row = 1;
                for (final DataModel dataModel : result) {
                    Anchor group = new Anchor(dataModel.getName());
                    grid.setWidget(row, 0, group);
                    final Anchor publisher = new Anchor();
                    group.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            Viewer viewer = new DataViewer(dataModel, null) {

                                @Override
                                protected boolean isPublish() {
                                    return false;
                                }
                                
                            };
                            viewer.update(dataModel.getKey());
                            grid.setWidget(0, 1, viewer);
                        }
                    });
                    grid.setWidget(row, 1, publisher);
                    LvUserService.Util.getInstance().getUser(dataModel.getOwner(), new AsyncCallback<UserModel>() {

                        public void onFailure(Throwable caught) {
                            // TODO Auto-generated method stub
                            
                        }

                        public void onSuccess(UserModel result) {
                            publisher.setText(result.getFirstName()+"," + result.getLastName());
                        }
                        
                    });
                    row++;
                }
            }
        });
        return grid;*/
    }

    private Widget getOwnerPanel(GroupModel groupModel) {
        Grid grid = new Grid();
        grid.resize(2, 1);
        grid.setWidget(0, 0, new HTML("<b>Owner</b>"));
        final HTML html = new HTML();
        grid.setWidget(1, 0, html);
        service.getUser(groupModel.getOwner(), new AsyncCallback<UserModel>() {
            public void onFailure(Throwable caught) {
                // 
                
            }

            public void onSuccess(UserModel result) {
                html.setHTML(result.getFirstName() + "," + result.getLastName());
            }       
        });
        return grid;
    }

    private Widget getUserList(final GroupModel groupModel) {
        final Grid grid = new Grid();
        
        service.getUsers(groupModel.getUsers(), new AsyncCallback<List<UserModel>>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(List<UserModel> result) {
                grid.resize(result.size()+2, 4);
                grid.setWidget(0, 0, new HTML("<b>Member</b>"));
                int row = 1;
                for (UserModel user : result) {
                    grid.setWidget(row, 0, new Anchor(user.getFirstName() + "," + user.getLastName()));
                    //grid.setWidget(row, 1, new Anchor(user.getEmail()));
                    row++;
                }
                
                grid.setWidget(row, 0, new Button("Leave", new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        DataService.Util.getInstance().leaveGroup(groupModel.getKey(), new AsyncCallback<Void>() {

                            public void onFailure(Throwable caught) {
                                // TODO Auto-generated method stub
                                
                            }

                            public void onSuccess(Void result) {
                                onBack();
                            }
                            
                        });
                    }
                }));
            }
            
        });
        return grid;
    }
}
